package rpc
import (
	//"context"
	//"google.golang.org/grpc"
    "time"

)
// 请求结构体
type DBRequest struct {
    RequestID      string      `json:"request_id"`
    Operation      string      `json:"operation"` //select,insert,update,delete,create_table
    Database       string      `json:"database"`
    Table          string      `json:"table"`
	//表名有：user,video,follow,likerecord,favorite,comment,tag,videotag,actionlog
    Data			map[string]any	`json:"data,omitempty"`
	Condition		map[string]any	`json:"condition,omitempty"`
	Fields      []string               `json:"fields,omitempty"`       // 查询的字段
	Limit       int                    `json:"limit,omitempty"`        // 查询限制
	Offset      int                    `json:"offset,omitempty"`       // 查询偏移
   // Timestamp      tfime.Time   `json:"timestamp"`
}
//操作特定的请求结构体
type InsertRequest struct{
	Table string		`json:"table"`
	TargetTable interface{} `json:"data"`
	
}
type  UpdateRequest struct{
	Table     string                 `json:"table"`
	Condition map[string]interface{} `json:"condition"`
	Data      map[string]interface{} `json:"data"` 		
}
type DeleteRequest struct {
	Table     string                 `json:"table"`
	Condition map[string]interface{} `json:"condition"`
}
type QueryRequest struct {
	Table     string                 `json:"table"`
	Condition map[string]interface{} `json:"condition,omitempty"`
	Fields    []string               `json:"fields,omitempty"`
	Limit     int                    `json:"limit,omitempty"`
	Offset    int                    `json:"offset,omitempty"`
	OrderBy   string                 `json:"order_by,omitempty"`
}
// 响应结构体
type DBResponse struct {
    RequestID     string        `json:"request_id"`
    Status        string        `json:"status"`
    Data          []interface{} `json:"data,omitempty"`
    AffectedRows  int64         `json:"affected_rows,omitempty"`
    LastInsertID  int64         `json:"last_insert_id,omitempty"`
    ErrorMessage  string        `json:"error_message,omitempty"`
    ExecutionTime int64         `json:"execution_time_ms"`
    NodeID        string        `json:"node_id"`
}
// 用户表 User
// ShardKeyer 定义了获取分片键的接口
type ShardKeyer interface {
    GetShardKey() int
}

type User struct {
    UserID       int     `json:"user_id" db:"user_id"`
    Username     string    `json:"username" db:"username"`
    PasswordHash string    `json:"-" db:"password_hash"` // 不序列化到JSON
    Nickname     string    `json:"nickname" db:"nickname"`
    AvatarURL    string    `json:"avatar_url" db:"avatar_url"`
    Gender       int       `json:"gender" db:"gender"` // 0:未知 1:男 2:女
    Bio          string    `json:"bio" db:"bio"`
    Phone        string    `json:"phone" db:"phone"`
    Email        string    `json:"email" db:"email"`
    RegisterTime time.Time `json:"register_time" db:"register_time"`
    LastLogin    time.Time `json:"last_login" db:"last_login"`
    Status       string    `json:"status" db:"status"` // active/banned/deleted
}

// 视频表 Video
type Video struct {
    VideoID       int     `json:"video_id" db:"video_id"`
    UserID        int     `json:"user_id" db:"user_id"`
    Title         string    `json:"title" db:"title"`
    Description   string    `json:"description" db:"description"`
    VideoURL      string    `json:"video_url" db:"video_url"`
    CoverURL      string    `json:"cover_url" db:"cover_url"`
    Duration      float64   `json:"duration" db:"duration"`
    UploadTime    time.Time `json:"upload_time" db:"upload_time"`
    Visibility    string    `json:"visibility" db:"visibility"` // public/private/friend
    ViewCount     int64     `json:"view_count" db:"view_count"`
    LikeCount     int64     `json:"like_count" db:"like_count"`
    CommentCount  int64     `json:"comment_count" db:"comment_count"`
    FavoriteCount int64     `json:"favorite_count" db:"favorite_count"`
}

// 关注表 Follow
type Follow struct {
    FollowerID int     `json:"follower_id" db:"follower_id"` // 关注者ID
    FolloweeID int     `json:"followee_id" db:"followee_id"` // 被关注者ID
    FollowTime time.Time `json:"follow_time" db:"follow_time"`
}

// 点赞表 LikeRecord
type LikeRecord struct {
    UserID    int     `json:"user_id" db:"user_id"`
    VideoID   int     `json:"video_id" db:"video_id"`
    LikeTime  time.Time `json:"like_time" db:"like_time"`
}

// 收藏表 Favorite
type Favorite struct {
    UserID       int     `json:"user_id" db:"user_id"`
    VideoID      int     `json:"video_id" db:"video_id"`
    FavoriteTime time.Time `json:"favorite_time" db:"favorite_time"`
}

// 评论表 Comment
type Comment struct {
    CommentID   int     `json:"comment_id" db:"comment_id"`
    VideoID     int     `json:"video_id" db:"video_id"`
    UserID      int     `json:"user_id" db:"user_id"`
    Content     string    `json:"content" db:"content"`
    CommentTime time.Time `json:"comment_time" db:"comment_time"`
    ParentID    *int64    `json:"parent_id,omitempty" db:"parent_id"` // 指针类型，可为空
}

// 标签表 Tag
type Tag struct {
    TagID   int  `json:"tag_id" db:"tag_id"`
    TagName string `json:"tag_name" db:"tag_name"`
}

// 视频标签表 VideoTag
type VideoTag struct {
    VideoID int `json:"video_id" db:"video_id"`
    TagID   int `json:"tag_id" db:"tag_id"`
}

// 行为日志表 ActionLog
type ActionLog struct {
    LogID      int     `json:"log_id" db:"log_id"`
    UserID     int     `json:"user_id" db:"user_id"`
    VideoID    *int    `json:"video_id,omitempty" db:"video_id"` // 指针类型，可为空
    ActionType string    `json:"action_type" db:"action_type"`     // view/like/comment/favorite等
    ActionTime time.Time `json:"action_time" db:"action_time"`
    DeviceInfo string    `json:"device_info" db:"device_info"`
    IPAddress  string    `json:"ip_address" db:"ip_address"`
}
//coordinator向node发送的RPC请求结构体
type CoordinatorRequest struct{
    TargetId    int//目标分片id,收到的机器进行检测，如果是leader则处理，如果不是则丢弃
    Data        map[string]any//表结构体
    Operation   string
    Condition   map[string]any
    TableName   string
}
type CoordinatorReply struct{
    ReplyId     int
    HandleOk    bool
    InsertSuccess bool
    QuerySuccess bool
    DeleteSuccess bool
    Operation  string
    Data        interface{}

}
// 常量定义
const (
	//节点状态
	NodeNum=3
    PieceNum=1
    // 用户状态
    UserStatusActive  = "active"
    UserStatusBanned  = "banned"
    UserStatusDeleted = "deleted"

    // 性别
    GenderUnknown = 0
    GenderMale    = 1
    GenderFemale  = 2

    // 视频可见性
    VisibilityPublic  = "public"
    VisibilityPrivate = "private"
    VisibilityFriend  = "friend"

    // 行为类型
    ActionTypeView      = "view"
    ActionTypeLike      = "like"
    ActionTypeComment   = "comment"
    ActionTypeFavorite  = "favorite"
    ActionTypeFollow    = "follow"
    ActionTypeShare     = "share"
    ActionTypeDownload  = "download"
	//表名
	UserTable ="user"
	ViderTable="video"
	FollowTable="follow"
	LikeRecordTable="likerecord"
	FavoriteTable="favorite"
	CommentTable="comment"
	TagTable="tag"
	VideoTagTable="videotag"
	ActionLogTable="actionlog"
	//工作节点的ipv4地址
	Node0Address="172.24.0.2"
	Node1Address=""
	Node2Address=""
)
// 获取用户分片键
func (u *User) GetShardKey() int {
	return u.UserID % NodeNum
}

// 获取视频分片键
func (v *Video) GetShardKey() int {
	return v.UserID % NodeNum
}

// 获取关注分片键
func (f *Follow) GetShardKey() int {
	return f.FollowerID % NodeNum
}

// 获取点赞分片键
func (l *LikeRecord) GetShardKey() int {
	return l.UserID % NodeNum
}

// 获取收藏分片键
func (f *Favorite) GetShardKey() int {
	return f.UserID % NodeNum
}

// 获取评论分片键
func (c *Comment) GetShardKey() int {
	return c.VideoID % NodeNum
}

// 获取行为日志分片键
func (a *ActionLog) GetShardKey() int {
	return a.UserID % NodeNum
}